var _phoneType = 'H5';
var _autoLocation = 0;
function thisAndroid(){
	_phoneType = 'Android';
	$(".html").hide();
	$(".android").show();
	if(_autoLocation == 0){
		if($("#locationDes2").length>0){
			$("#html").hide();
			$("#android").show();
			$("#locationDes2").html("正在获取位置......");
		}
	}
}

function setUserTrajectory(lng,lat){
	var url = window.rootPath + '/ui/userTrajectory/setUserTrajectory?lng='+lng+'&lat='+lat;
	jQuery.ajax({
		type : 'get',
		url : url,
		data : '',
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function() {
			
		}
	});
}

function thisIos(){
	_phoneType = 'Ios';
	$(".html").hide();
	$(".android").show();
	if(_autoLocation == 0){
		if($("#locationDes2").length>0){
			$("#html").hide();
			$("#android").show();
			$("#locationDes2").html("正在获取位置......");
		}
	}
}
function QRCodeUrl(url){
	window.location.href=url;
}
function getPhoneType(){
	return _phoneType;
}
function setAutoLocation(value){
	_autoLocation = value;
}
function getAutoLocation(){
	return _autoLocation;
}

(function () {
//	$("body").on("click", ".menuClick", function (){
////		var id=$(this).attr("id");
////		if(id=="menu-home"){
////			window.location.href = window.rootPath + "/ui/mobileMain?device=mobile&identify=";
////		}else{
////			var url = $(this).attr('href');
////			window.location.href = url;
////		}
//		var url = $(this).attr('href');
//		window.location.href = url;
//	});
	
	$("body").on("click", ".menuClick", function (){
		var accountName = $('#accountName').val();
		if(accountName == 'Visitors'){
			//confirmJQM('确认?', function(){alert('点击了确定')});
			var id=$(this).attr("id");
			if(id != "home"){
				if(confirm('请登陆')){
					window.location.href = window.rootPath + "/ui/login?device=mobile&identify=";
				}
				return false;
			}
		}
		
	});
	
	$("body").on("click", ".imgZoomIn", function (){
		$(this).ImgZoomIn();
	})
	
	function confirmJQM(text, callback) {
	    var popupDialogId = 'popupDialogC';
	    $('<div data-role="popup" id="' + popupDialogId + '" data-confirmed="no" data-position-to="window" data-transition="pop" data-theme="b" data-dismissible="false" style="max-width:500px;">'+  
	        '<div role="main" class="ui-content" style="text-align: center;">'+  
	            '<h3 class="ui-title">' + text + '</h3>'+  
	            '<p></p>'+
	            '<a data-role="button" class="optionCancel" data-mini="true" data-inline="true" onclick="$(\'#popupDialogC\').popup(\'close\');" >取消</a>'+ 
	            '<a data-role="button" class="optionConfirm" data-transition="flow" data-inline="true" data-mini="true">确定</a>'+  
	        '</div>'+  
	    '</div>').appendTo($.mobile.pageContainer);  
	    var popupDialogObj = $('#' + popupDialogId);  
	    popupDialogObj.trigger('create');  //动态加载时 需要重新刷新下 也就是给popup赋上jqm对应的css
	    
	    //初始化popup
	    popupDialogObj.popup({
	     //关闭时 绑定的事件
	        afterclose: function (event, ui) {  
	            popupDialogObj.find(".optionConfirm").first().off('click'); //关闭时 需要清除确定按钮上 绑定的事件
	            $(event.target).remove();//删除 创建的 popup
	        },
	        
	        //显示时 绑定的事件
	        afteropen: function (event, ui) {
	         popupDialogObj.find(".optionConfirm").first().on('click', function () {  
	          popupDialogObj.attr('data-confirmed', 'no'); 
	          $('#popupDialogC').popup('close');
	          if(callback && callback instanceof Function ){
	           callback();
	         }
	      });
	        }  
	    });  
	    //打开
	    popupDialogObj.popup('open');  
	 }

	$("body").on("click", ".goHome-dcms", function (){
		window.location.href = window.rootPath + "/ui/bussinessmain?device=mobile&identify="+$('#identify').val();
	});

	$("body").on("click", ".goHome", function (){
		window.location.href = window.rootPath + "/ui/mobileMain?device=mobile&identify=";
	});
	
	$("body").on("click", ".pageBack", function (){
		if(getPhoneType() == 'H5'){
			//window.location.href=document.referrer;
		}else if(getPhoneType() == 'Android'){
			window.jump.WebgoBack(); 
		}else if(getPhoneType() == 'Ios'){
			//window.location.href=document.referrer;
			//window.webkit.messageHandlers.goBack.postMessage({body: 'goBack'});
		}
	});

	$("body").on("click", ".goBack", function (){
		if(getPhoneType() == 'H5'){
			window.location.href=document.referrer;
//			window.history.back(-1);
		}else if(getPhoneType() == 'Android'){
			window.jump.WebgoBack(); 
		}else if(getPhoneType() == 'Ios'){
			//window.location.href=document.referrer;
			window.webkit.messageHandlers.goBack.postMessage({body: 'goBack'});
		}
		
//		var referrer = document.referrer;
//		var url = window.location.href;
//		var r = referrer.split("?");
//		var u = url.split("?");
//		
//		if(r[0] == u[0]){
//			if(u.length=2){
//				var t = u.split("&");
//				for(var i=0;i<t.length;i++){
//					if(getUrlParam(u[1],t[i].split("=")) != getUrlParam(r[1],t[i].split("="))){
//						alert(getUrlParam(u[1],t[i].split("=")) + '|' + getUrlParam(r[1],t[i].split("=")));
//						window.history.back(-1);
//					}
//				}
//				window.history.go(-3);
//			}else{
//				window.history.back(-1);
//			}
//		}else{
//			window.history.back(-1);
//		}
	});
	
	function getUrlParam(url,name){
        var reg=new RexExp("(^|&)"+name+"=([^&]*)(&|$)])");//构造一个含有目标参数的正则表达式对象        
        var r=url.match(reg);//匹配目标参数
        if(r!=null)return unescape(r[2]);return null;//返回参数值
    }
	
	$(document).ready(function() {
		getNewData();
	})
	/**
	 * 初始化数据-页面菜单
	 */
	function getNewData() {
		var uri = window.location.pathname;
		uri = uri.split("/")[3];
		var url = window.rootPath + '/ui/mobileMain/getData';
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				//#动态添加菜单
				var contenthtml='<ul id="menu-list" class="nav nav-tabs">';
//				contenthtml += '<li><a href="javascript: ;" class="menuClick"'
//					+'id="menu-home"><div><span class="fa fa-home" style="font-size:20px;"></span></div><lable>首页</lable></a></li>'
//					+'<li><a href="javascript: ;"  class="menuClick"'
//					+'id="menu-login"><div><span class="fa fa-address-book-o" style="font-size:20px;"></span></div><lable>通讯录</lable></a></li>'
					//加特效
				var mobileId = '';
				for(var i=0;i<data.modules.length;i++){
					if(data.modules[i].mobile!=undefined && data.modules[i].mobile=="true"){//&&data.modules[i].menuLevel==1
						if (data.modules[i].menuLevel==1){
							mobileId = data.modules[i].id;
						}else if (data.modules[i].menuLevel==2 && mobileId == data.modules[i].parentId){
							var url='javascript: ;';
							var icon="home";
							if(data.modules[i].icon!=undefined&&data.modules[i].icon!=""){
								icon=data.modules[i].icon;
							}
							if(data.modules[i].target!="#"){
								if(data.modules[i].target.indexOf("?") != -1){
									url=window.rootPath+data.modules[i].target+"&device=mobile&identify="+$("#identify").val();
								}else{
									url=window.rootPath+data.modules[i].target+"?device=mobile&identify="+$("#identify").val();
								}
								if(url.indexOf(uri) != -1){
									contenthtml +='<li><a href="'+url+'" data-ajax="false" class="menuClick "'
										+'id="'+icon+'"><div><span class="fa fa-'+icon+' menu-active" style="font-size:20px;"></span></div><lable class="menu-active">'+data.modules[i].moduleName+'</lable></a></li>';
								}else{
									contenthtml +='<li><a href="'+url+'" data-ajax="false" class="menuClick"'
										+'id="'+icon+'"><div><span class="fa fa-'+icon+'" style="font-size:20px;"></span></div><lable>'+data.modules[i].moduleName+'</lable></a></li>';
								}
							}else{
								//加特效
								contenthtml +='<li><a href="javascript: ;"/></li>';
							}
							
						}
					}
				}
				contenthtml+='</ul>';
				$('#menu-bar').empty();
				$('#menu-bar').append(contenthtml);
				
				$('.flyout').empty();
				var flyout = "";
				for(var i=0;i<data.eventTypes.length;i++){
					var icon = data.eventTypes[i].iconAddress;
					if(icon && icon != ''){
						icon = "/wcsi/" + icon;
					}else{
						icon = "/wcsi/img/defaultEventType.png";
					}
//					flyout += '<li class="flyout-init fade"><a class="swipe" href="#" data-id="'+data.eventTypes[i].id+'"><span style="background:url(\''+icon+'\') no-repeat center center; background-size:4em 4em; width: 4em;height: 4em;"></span></a></li>';
					var eventName = data.eventTypes[i].eventName;
					flyout += '<li class="flyout-init fade"><a class="swipe" href="#" data-id="'+data.eventTypes[i].id+'"><span><font>'+eventName.substr(eventName.length-1,1)+'</font></span></a></li>';
				}
				$('.flyout').append(flyout);
				eachItem();
			}
		})
	}
	
	
	var tempClick = false;
	var num = 1;
	$(".flyout-btn").click(function() {
		if(tempClick){
			setTimeout(function(){
				hideBg();
			},500);
			tempClick = false;
		}else{
			shwoBg();
			tempClick = true;
		}
		$(".flyout-btn").toggleClass("btn-rotate");
		var len = $(".flyout li").length;
		if(len < 4){//小于4个 使用原处理方式
			if($(".flyout li").hasClass("flyout-init")){
				$(".flyout li").removeClass("flyout-init fade");
				$(".flyout li").toggleClass("expand");
			}else{
				$(".flyout li").toggleClass("expand");
				setTimeout(function(){
					$(".flyout li").addClass("flyout-init fade");
				},400);
			}
		}else{
			if(tempClick){
				num = 1;
				var rotate = 45;
				for(var i=0;i<3;i++){
					var value = rotate * (i+1) - 90;
					$(".flyout li").eq(i).css("transform","rotate("+value+"deg)");
					$(".flyout li").eq(i).removeClass("flyout-init fade");
					$(".flyout li").eq(i).toggleClass("expand");
					$(".flyout li a").eq(i).rotate({animateTo: (1-i)*rotate}); 
				}
			}else{
				$(".expand").removeClass("expand");
				setTimeout(function(){
					$(".flyout li").addClass("flyout-init fade");
					$(".flyout li").removeAttr("style")
					$(".flyout li").removeClass("leftrotate1").removeClass("leftrotate2").removeClass("rightrotate1").removeClass("rightrotate2");
				},400);
			}
		};
		return;
	});

	/**
	 * item 点击
	 */
	$("body").on("click", ".flyout a", function (){
		var dataId = $(this).attr("data-id");
		$(".flyout-btn").toggleClass("btn-rotate");
		$(this).addClass("clicked");
		$(".expand").removeClass("expand").addClass("fade");
		setTimeout(function(){
			$(".flyout li").addClass("flyout-init");
			window.location.href = window.rootPath + "/ui/eventReport?device=mobile&eventType="+dataId+"&identify=";
			
		},400);
		
	});
	
//	function eachItem(){
//		var len = $(".flyout li").length;
//		var rotate = 180/(len+1);
//		$(".flyout li").each(function(index){
//			var value = rotate * (index+1) - 90;
//			$(this).css("transform","rotate("+value+"deg)");
//		});
//	}

	function eachItem(){
		var len = $(".flyout li").length;
		if(len < 4){//小于4个 使用原处理方式
			var rotate = 180/(len+1);
			$(".flyout li").each(function(index){
				var value = rotate * (index+1) - 90;
				$(this).css("transform","rotate("+value+"deg)");
				$(this).find("a").rotate(0-value);
			});
		}else{
			var rotate = 45;
			for(var i=0;i<3;i++){
				var value = rotate * (i+1) - 90;
				$(".flyout li").eq(i).css("transform","rotate("+value+"deg)");
				$(".flyout li a").eq(i).rotate({animateTo: (1-i)*rotate}); 
			}
		}
		
	}
	// 左滑动
	$("body").on("swipeleft",".swipe",function(){
		var len = $(".flyout li").length;
		if(len < 4){
			return;
		}
		if(num == ($(".flyout li").length - 2) ){
			return;
		}
		num += 1;
		var first = $(".expand").eq(0);
		first.removeClass("expand");
		setTimeout(function(){
			first.addClass("flyout-init fade");
			$(".flyout li").each(function(index){
				if(index == (num-1)){
					$(this).addClass("leftrotate1");
					$(this).removeClass("leftrotate2");
					$(this).removeClass("rightrotate1");
					$(this).find("a").rotate({
						animateTo: 45,
						duration: 400
					});
				}
				if(index == num){
					$(this).addClass("leftrotate2");
					$(this).removeClass("rightrotate2");
					$(this).find("a").rotate({
						animateTo: 0,
						duration: 400
					});
				}
				if(index == (num+1)){
					$(this).css("transform","rotate(45deg)");
					$(this).removeClass("flyout-init fade");
					$(this).addClass("expand");
					$(this).find("a").rotate(-45);
				}
			});
		},400);
	});

	// 右滑动
	$("body").on("swiperight",".swipe",function(){
		var len = $(".flyout li").length;
		if(len < 4){
			return;
		}
		if(num == 1){
			return;
		}
		num -= 1;
		var last = $(".expand").eq(2);
		last.removeClass("expand");
		setTimeout(function(){
			last.addClass("flyout-init fade");
			$(".flyout li").each(function(index){
				if(index == (num-1)){
					$(this).css("transform","rotate(-45deg)");
					$(this).removeClass("flyout-init fade");
					$(this).addClass("expand");
					
				}
				if(index == num){
					$(this).addClass("rightrotate1");
					$(this).removeClass("leftrotate1");
					$(this).find("a").rotate({
						animateTo: 0,
						duration: 400
					});
				}
				if(index == (num+1)){
					$(this).addClass("rightrotate2");
					$(this).removeClass("rightrotate1");
					$(this).removeClass("leftrotate2");
					$(this).find("a").rotate({
						animateTo: -45,
						duration: 400
					});
				}
			});
		},400);
	});
	
	//显示屏蔽层
	function shwoBg(){
		$(".BgDiv").css({ display: "block", height: $(document).height() - $('#footerMenu').height()});
		$('html,body').addClass('ovfHiden');
	}
	
	function hideBg(){
		$('html,body').removeClass('ovfHiden');
		$(".BgDiv").css("display", "none");
	}
	
	$('aside').on('touchmove', function(event) {
	    event.preventDefault();
	});
	
})();